package com.ocean.greedy.easy;

/**
 * https://leetcode.cn/problems/valid-palindrome-ii/
 * 验证回文串 II
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2023/02/20 <br>
 */
public class ValidPalindromeII {
    public static void main(String[] args) {
        ValidPalindromeII palindromeII = new ValidPalindromeII();
        boolean res = palindromeII.validPalindrome("aba");
        System.out.println(res);
        res = palindromeII.validPalindrome("abca");
        System.out.println(res);
        res = palindromeII.validPalindrome("abc");
        System.out.println(res);
        res = palindromeII.validPalindrome("abcdba");
        System.out.println(res);
    }

    public boolean validPalindrome(String s) {
        if (s.length() == 1) {
            return true;
        }
        int low = 0;
        int high = s.length() - 1;
        while (low < high) {
            char l_c = s.charAt(low);
            char h_c = s.charAt(high);
            if (l_c == h_c) {
                low++;
                high--;
            } else {
                return validPalindrome(s, low + 1, high) || validPalindrome(s, low, high - 1);
            }
        }
        return true;
    }

    private boolean validPalindrome(String s, int low, int high) {
        for (; low < high; low++, high--) {
            if (s.charAt(low) != s.charAt(high)) {
                return false;
            }
        }
        return true;
    }
}
